﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hejian_PCBA_Test.Procdure
{
    internal class TestConfig
    {
        /// <summary>
        /// 偏差
        /// </summary>
        public double Offset { get; set; }

        /// <summary>
        /// 测试延时
        /// </summary>
        public int SleepTime { get; set; }

        /// <summary>
        /// tp点
        /// </summary>
        public string TP { get; set; }

        /// <summary>
        /// 接地继电器编号
        /// </summary>
        public int GndRelayNo { get; set; }

        /// <summary>
        /// 继电器编号
        /// </summary>
        public int RelayNo { get; set; }

        /// <summary>
        /// 测试下限
        /// </summary>
        public double LowerLimit { get; set; }

        /// <summary>
        /// 上限
        /// </summary>
        public double UpperLimit { get; set; }

        public static List<TestConfig> LoadConfig(string csvPath)
        {
            List<TestConfig> configs = new List<TestConfig>();
            var tmp = ReadCsvFile(csvPath);
            foreach (var config in tmp)
            {
                if (config.Length != 7)
                {
                    throw new Exception($"{csvPath}解析异常");
                }
                if (config[0] == "TP")
                {
                    continue;
                }

                var singConfig = new TestConfig
                {
                    TP = config[0],
                    RelayNo = int.Parse(config[1]),
                    GndRelayNo = int.Parse(config[2]),
                    LowerLimit = double.Parse(config[3]),
                    UpperLimit = double.Parse(config[4]),
                    SleepTime = (int)(double.Parse(config[5]) * 1000),
                    Offset = double.Parse(config[6])
                };

                configs.Add(singConfig);
            }
            return configs;
        }

        private static string[][] ReadCsvFile(string eepromFilePath)
        {
            if (!File.Exists(eepromFilePath))
                throw new Exception($"未找到文件[{eepromFilePath}]");
            // 读取 CSV 文件的所有行
            string[] lines = File.ReadAllLines(eepromFilePath);

            // 创建一个二维字符串数组来存储 CSV 数据
            string[][] csvData = new string[lines.Length][];

            // 解析每一行并存储到数组中
            for (int i = 0; i < lines.Length; i++)
            {
                csvData[i] = lines[i].Split(',');
            }

            return csvData;
        }
    }
}